<script src="{{ getAssets }}js/amazeui.datatables.min.js?v={{ assets_version }}"></script>
<script>
    var tables = $('#dataTables').DataTable({
        "sAjaxSource" : '{{ urlfor "CollectfeesController.GetWaitDispensingUser"}}'
        ,"fnServerData" : function(sSource, aoData, fnCallback) {
            var params = checkQueryTableData(aoData);
            $.post(sSource, {'kwd':$('#search_key').val()},function(resp) {
                fnCallback(resp);
            },'json')
        }
        ,columns: [
            {"bSortable": false, render:function(data, type, full, meta) {
                    return full.archives.Realname;
            }}
            ,{"bSortable": false, render:function(data, type, full, meta) {
                    return full.archives.Sex;
            }}
            ,{"bSortable": false, render:function(data, type, full, meta) {
                    return full.archives.Age;
            }}
        ]
        ,"aLengthMenu":[100]
        ,'bLengthChange': false
        ,"serverSide": true
        ,"bPaginate" : false
        ,"bFilter" : false	//是否启动过滤、搜索功能
        ,"bInfo" : false //是否显示页脚信息，DataTables插件左下角显示记录数
        ,"bProcessing" : true //DataTables载入数据时，是否显示‘进度’提示
    });
    $("#searchsubmit").bind("click", function () { //点击按钮 触发table重新请求服务器
        $("#dataTables").dataTable().fnDraw(false);
    });

    $(window).keydown(function (event) {
        if(event.keyCode==13){
            var value = $('#search_key').val();
            if(value != '') {
                $("#searchsubmit").click();
            }
        }
    });
    var sel_member; //选择的患者
    //待诊列表tr点击
    $('#dataTables tbody').on( 'click','tr',function(){
        var data = tables.row(this).data();
        $('#dataTables tbody').find('tr').removeClass('am-active');
        $(this).addClass('am-active');

        if(data.archives) {
            var uinfo = data.archives ;
            var main_obj = $('.userinfo');
            main_obj.find('.name').html(uinfo.Realname);
            main_obj.find('.sex').html(uinfo.Sex);
            main_obj.find('.age').html(uinfo.Age+'岁');
            main_obj.find('.mobile').html(uinfo.Mobile);
            main_obj.find('.birthday').html(uinfo.Birthday);
        }
        sel_member = data;
        doctorOrderQuery();
    });
    var dataTablesDoctorOrder = $('#dataTablesDoctorOrder').DataTable({
        order:[]
        ,columns: [
            {"bSortable": false,"width":80, "render":function (data, type, full, meta) {
                    return '<input type="checkbox" class="sel-do-list" value="'+ full.id +'" checked data-price="'+ full.total_price +'">';
            }},
            {"mDataProp" : "status", "bSortable": false, "render":function (data, type, full, meta) {
                    var xu = '';
                    var pay = '';
                    switch (parseInt(full.pay_status)) {
                        case 0:pay ='未支付';break;
                        case 1:pay ='已支付';break;
                        case 2:pay ='待退费';break;
                        case 3:pay ='已退费';break;
                    }
                    switch (parseInt(data)) {
                        case 0:xu ='<span>待发送</span> ';break;
                        case 1:xu ='<span class="am-text-secondary">已发送('+ pay +')</span> ';break;
                        case 2:xu ='<span class="am-text-warning">已回退('+ pay +')</span> ';break;
                        case 3:xu ='<span class="am-text-danger">已退费('+ pay +')</span>';break;
                        case 4:xu ='<span style="color: gray">已作废('+ pay +')</span>';break;
                    }

                    return '<span class="number" style="white-space:nowrap;" data-index="'+ meta.row +'" data-number="'+ full.number.id +'" data-prescdate="'+ timetransDate(full.number.created_at) +'">'+xu+'</span><input type="hidden" class="project_type" value="'+ full.project_type +'"><input type="hidden" class="total_price" value="'+ full.total_price +'">';
            }},
            {"mDataProp" : "content", "bSortable": false, "render":function (data, type, full, meta) {
                    return '<span class="content">'+ data +'</span>';
             }},
            {"mDataProp" : "dosage_zl", "bSortable": false,"render":function (data, type, full, meta) {
                    return '<span class="dosage_zl">'+ data +'</span>';
             }},
            {"mDataProp" : "unit", "bSortable": false,"render":function (data, type, full, meta) {
                    return '<span class="unit">'+ data +'</span>';
            }},
            {"bSortable": false, render:function(data, type, full, meta) {
                    return '<span class="usage">'+(full.position ? full.position +'，':'')+ showUsageFormat(full.usage_tj, full.usage_pc, full.dosage_ts)+'</span>';
            }},
            {"mDataProp" : "created_at", "bSortable": false,"render":function (data, type, full, meta) {
                    return '<span class="created_at">'+ timetrans(data) +'</span>';
            }}
        ]
        ,'bLengthChange': false
        ,"serverSide": false
        ,"bPaginate" : false
        ,"bFilter" : false
        ,"bInfo" : false
        ,"bProcessing" : false
        ,"drawCallback": function ( settings ) {

        }
    });

    $('#dataTablesDoctorOrder tbody').on( 'click','tr',function(){
        var chk = $(this).find('input[type="checkbox"]');
        if(chk.prop('checked')) {
            chk.prop('checked', false);
            chk.parent().parent().addClass('gray');
        } else {
            chk.prop('checked', true);
            chk.parent().parent().removeClass('gray');
        }
    });

    //查询处方列表
    function doctorOrderQuery() {
        if(!sel_member) {
            return false;
        }
        $.post('{{urlfor "PrescController.DoctorOrderQuery"}}',JSON.stringify({'archives_id':sel_member.archives.Id, 'pay_status':'1', 'status':'1', 'project_type':'drug', 'drug_send':"0"}), function(res){
            if(res.code == 1) {
                return false;
            }
            var dt = $('#dataTablesDoctorOrder').DataTable();
            dt.clear().draw();
            $.each(res.data, function (index, value) {
                dt.row.add(value).draw();
            });
        }, 'json');
    }
    $('#setDispensing').click(function () {
        if(!sel_member) {
            layer.alert('患者信息不存在');return false;
        }
        var doctor_ids = [];
        $('#dataTablesDoctorOrder tbody').find('.sel-do-list').each(function (idx ,val) {
            if($(this).prop('checked')) {
                doctor_ids.push($(this).val());
            }
        });
        if(doctor_ids.length <= 0){
            layer.alert('请选择待发药品');return false;
        }
        var save_data = {
            'archives_id':sel_member.archives.Id,
            'doctor_ids' : doctor_ids.join(',')
        };

        if(confirm("确定执行发药吗？")) {
            $.post('{{  urlfor "CollectfeesController.Dispensing" }}',save_data, function (res) {
                if(res.code == 1) {
                    layer.alert(res.msg); return false;
                }
                layer.alert(res.msg,function (index) {
                    $('#dataTablesDoctorOrder').DataTable().clear().draw();
                    $("#searchsubmit").click();
                    layer.close(index);
                });
            }, 'json').error(function () {
                layer.alert('发药失败请重试');
            });
        }

        return false;
    });

</script>